<template>
  <div>
    <van-nav-bar title="黑马头条-登录" />
    <!-- 表单区域 -->
    <van-form @submit="onSubmit">
      <van-field
        required
        v-model="form.mobile"
        label="手机号"
        placeholder="请填写手机号"
        :rules="[
          {
            required: true,
            message: '请填写手机号',
            trigger: 'onChange',
            pattern: /^1[3456789]\d{9}$/,
          },
        ]"
      />
      <van-field
        v-model="form.code"
        type="password"
        required
        label="验证码"
        placeholder="请填写验证码"
        :rules="[
          {
            required: true,
            message: '请填写正确验证码',
            trigger: 'onChange',
            pattern: /^\d{6}$/,
          },
        ]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
        :disabled="isLogin"
          >立即登录</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { loginAPI } from '@/api'
import { Notify } from 'vant'
import { setToken } from '@/utils/token.js'
export default {
  data () {
    return {
      form: {
        mobile: '17835208419',
        code: '246810'
      },
      isLogin: false
    }
  },
  methods: {
    async onSubmit () {
      try {
        this.isLogin = true
        // console.log('submit', values)
        const res = await loginAPI(this.form)
        console.log(res)
        Notify({ type: 'success', message: '登陆成功' })
        this.isLogin = false
        setToken(res.data.data.token)
        this.$router.push('/layout')
      } catch (err) {
        console.error(err)
        console.log(111)
        this.isLogin = false
      }
    }
  }
}
</script>

<style>
</style>
